<?php
/**
 * AJAX Handler for Site: Add - Adds a site into the database
 * NOTE:    A Site has been standardised from Websites, Documentation Links, and Blog Links.
 *	    This is because they are all in the same format. A Change here affects all 3.
 * 
 * I have made sure Libdebug is silent in AJAX. This is because it can intefere with the HTML/Javascript as Libdebug Appends after </html>.
 * If you have a problem and need to debug comment $_LIBDEBUG->silence(); (the framework must also be in debugging mode for this to work)
 */

    $_ECLIPSEMDE = array();
    
    // Disable Templating System
    $_ECLIPSEMDE['TEMPLATING'] = false;
    require_once('../../SiteIncludes.GLOBAL.php');
    
    // Tell libdebug to never print out error information - overrides debugging flags in fwork
    $_LIBDEBUG->silence();
    
    if(EclipseMDE::runningOnMod_Rewrite() === false) {
	// Security Guard, Do Not Let This be accessed by any other means than mod_rewrite
	header("HTTP/1.1 400 Bad Request");
	die($nok_caller);
    }
    
    if(isset($gVAR['type']) === true) {
	$lister = 'parent.SiteEDJX.list(\''.$gVAR['type'].'\');';
    } else {
	$lister = '';
    }
    $return_ok = <<<EOOK
	<!DOCTYPE html>
	    <html>
	    <head><title></title></head>
	    <body>
	    <script type="text/javascript">
		parent.EDJXConfirmer.show_ok();
		$lister
	    </script>
	    </body>
	</html>
EOOK;
    $return_nok = <<<EONOK
    <!DOCTYPE html>
	    <html>
	    <head><title></title></head>
	    <body>
	    <script type="text/javascript">
		parent.EDJXConfirmer.show_nok();
		$lister
	    </script>
	    </body>
	</html>
EONOK;
    if(isset($pVAR['submit_item']) === true) {
	$pVAR['add_item'] = $pVAR['submit_item'];
    }
    if(isset($_LOGGEDINUSER) === true && $_LOGGEDINUSER !== false && isset($pVAR['add_item']) !== false && isset($gVAR['type']) !== false && isset($gVAR['alias']) !== false) {
	try {
	    $Project =& $ProjectStash->get($gVAR['alias']);
	    $url = $pVAR['add_item'];
	    $VALID_URL = filter_var($url, FILTER_VALIDATE_URL);
	    if($Project->userCanEdit($_LOGGEDINUSER->get_userid()) === true && $VALID_URL !== false) {
		$safe_url = $sql_conx->real_escape_string($url);
		if($gVAR['type'] == 'site') {
		   $vals = $Project->getWebsites();
		   $sql_table = 'tool_websites';
		   $sql_column = 'website';
		   $type_prjid = ($Project->isMaintainer($_LOGGEDINUSER->get_userid()) === true ? ProjectEditType::AddSite : ProjectEditType::AdminAddSite);
		} elseif($gVAR['type'] == 'docs') {
		    $vals = $Project->getDoclinks();
		    $sql_table = 'tool_doclinks';
		    $sql_column = 'doclink';
		    $type_prjid = ($Project->isMaintainer($_LOGGEDINUSER->get_userid()) === true ? ProjectEditType::AddDocs : ProjectEditType::AdminAddDocs);
		} elseif($gVAR['type'] == 'blog') {
		    $vals = $Project->getBlogs();
		    $sql_table = 'tool_blogs';
		    $sql_column = 'blogurl';
		    $type_prjid = ($Project->isMaintainer($_LOGGEDINUSER->get_userid()) === true ? ProjectEditType::AddBlog : ProjectEditType::AdminAddBlog);
		} elseif($gVAR['type'] == 'rss') {
		    $vals = $Project->getRSSfeeds();
		    $sql_table = 'tool_rssfeeds';
		    $sql_column = 'rssuri';
		    $type_prjid = ($Project->isMaintainer($_LOGGEDINUSER->get_userid()) === true ? ProjectEditType::AddRSS : ProjectEditType::AdminAddRSS);
		} else {
		    header("HTTP/1.1 400 Bad Request");
		    die();
		}
		if(in_array($url, $vals) === true) {
		    // Already Exists!
		} else {
		    // Add
		    $query = $sql_conx->query("INSERT INTO `{$sql_table}` ( `pid` , `{$sql_column}` ) VALUES ( '{$Project->getID()}',  '{$safe_url}' )");
		    if($sql_conx->affected_rows === 0) {
			$sendHeader = "HTTP/1.1 500 Internal Server Error";
			$calls = <<<EOC
			    <script type="text/javascript">
				//parent.SiteEDJX.errorOnAdd();
				parent.EDJXConfirmer.show_nok();
				$lister
			    </script>
EOC;
		    } else {
			$Project->updateTriggered();
			$Project->updateWrite($_LOGGEDINUSER->get_userid(), $type_prjid, array($pVAR['add_item']));
			$sendHeader = "HTTP/1.1 200 OK";
			$calls = <<<EOC
			    <script type="text/javascript">
				parent.EDJXConfirmer.show_ok();
				$lister
			    </script>
EOC;
		    }
		    header($sendHeader);
		    $calls = '<!DOCTYPE html><html><head><title></title></head><body>'.$calls.'</body></html>';
		    die($calls);
		}
	    } else {
		throw new InvalidProjectException();
	    }
	} catch(InvalidProjectException $e) {
	    header("HTTP/1.1 400 Bad Request");
	    die($return_nok);
	}
    } else { 
	header("HTTP/1.1 400 Bad Request");
	die($return_nok);
    }
    ?>